package com.example.second.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.second.dao.IOrderDao;
import com.example.second.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
public class OrderController {

    @Autowired
    IOrderService iOrderService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/createorder/{sku_id}")
    public Map<String, Object> createOrder(@PathVariable String sku_id, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        Map<String, Object> resultMap = new HashMap<String, Object>();


        String user_id="";
        if (httpServletRequest.getCookies()!=null){
            for( Cookie cookie:httpServletRequest.getCookies()){

                String sessionid = cookie.getValue();
                String session = stringRedisTemplate.opsForValue().get(sessionid);
                if(session==null){
                    continue;
                }
                Map<String,Object> usermap = JSON.parseObject(stringRedisTemplate.opsForValue().get(sessionid), Map.class);

                if(!usermap.isEmpty()&&usermap.get("user_id")!=null){
                    user_id = usermap.get("user_id").toString();
                    break;
                }
            }
        }


        if(user_id.equals("")){
            resultMap.put("result", false);
            resultMap.put("msg", "会员没有登录不能购买！");
            return resultMap;
        }

        resultMap = iOrderService.createOrder(sku_id,user_id);
        return resultMap;

    }

    @RequestMapping(value = "/getorder/{order_id}")
    public Map<String, Object> getOrder(@PathVariable("order_id") String order_id){
        return iOrderService.getOrder(order_id);
    }

    @RequestMapping(value = "/payorder/{order_id}/{sku_id}")
    public Map<String, Object> payOrder(@PathVariable("order_id") String order_id,@PathVariable("sku_id") String sku_id){
        //调用支付接口
        boolean isPay = true;
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if(!isPay){
            resultMap.put("result", false);
            resultMap.put("msg", "请先支付");
            return resultMap;
        }
        return iOrderService.payOrder(order_id, sku_id);
    }
}
